<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="8"
              type="flex"
              align="middle"
              style="margin-bottom:10px">

        <el-col :span="1.5">
          <el-button size="mini"
                     type="primary"
                     icon="el-icon-plus"
                     @click="add">新增</el-button>
        </el-col>

      </el-row>
      <el-table :data="tableData"
                v-loading="loading"
                style="width: 100%"
                :header-cell-style="{background:'#f8f8f9','text-align':'center'}"
                :cell-style="{'text-align':'center'}">

        <el-table-column prop="configValue"
                         label="值">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px"
                      :src="scope.row.configValue"
                      :preview-src-list="[scope.row.configValue]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="sort"
                         label="排序">
        </el-table-column>
        <el-table-column prop="mk"
                         label="备注">
        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="data">
            <el-button icon="el-icon-edit"
                       type="text"
                       @click="updateHandler(data.row.textConfigId)">修改</el-button>
            <el-button icon="el-icon-delete"
                       type="text"
                       @click="deleteHandler(data.row.textConfigId)">删除</el-button>
          </template>

        </el-table-column>

      </el-table>
      <div class="page"
           v-show="total!=0">
        <el-pagination background
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryParams.pageNum"
                       :page-sizes="[3, 5, 7, 9]"
                       :page-size="queryParams.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </el-card>

    <div>
      <el-dialog :title="flag?'添加系统文本配置':'修改系统文本配置'"
                 :visible.sync="dialogVisible"
                 width="30%">
        <el-form :model="formData"
                 ref="ruleForm"
                 label-width="80px">
          <el-form-item label="轮播图">
            <el-upload class="avatar-uploader"
                       action="https://jumeng.xzjumeng.com/prod-api/common/upload"
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess">
              <img v-if="formData.configValue"
                   width="100px"
                   height="100px"
                   :src="formData.configValue"
                   class="avatar">
              <i v-else
                 class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="轮播详情">
            <quill-editor :gz="gz"
                          @editorchange='getEidtorText' />

          </el-form-item>
          <el-form-item label="排序">
            <el-input size="small"
                      v-model="formData.sort"
                      placeholder="排序"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input size="small"
                      v-model="formData.mk"
                      placeholder="备注"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="update">确 定</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import quillEditor from '../../../components/Quill'
export default {
  components: { quillEditor },
  data() {
    return {
      queryParams: { pageNum: 1, pageSize: 3 },
      tableData: [],
      total: 0,
      loading: false,
      formData: {
        configCode: 'lb',
        configImg: '',
        configName: null,
        configType: 3,
        configValue: '',
        configwenben: '',
        createTime: null,
        mk: null,
        remark: null,
        sort: null,
        textConfigId: null,
        tzValue: '',
        updateTime: null
      },
      dialogVisible: false,
      gz: '',
      flag: true
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getData()
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getData()
    },
    async getData() {
      this.loading = true
      let { data } = await this.$axios.get('com/textConfig/listlb', { params: this.queryParams })
      console.log(data)
      this.tableData = data.rows
      this.loading = false
      this.total = data.total
    },
    deleteHandler(id) {
      this.$confirm(`是否确认删除意见编号为${id.toString()}的数据项?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.del(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async del(id) {
      let { data } = await this.$axios.delete(`com/textConfig/${id}`)
      if (data.code != 200) return this.$message.error(data.msg)
      this.$message({
        type: 'success',
        message: data.msg
      })
      this.getData()
    },

    async updateHandler(id) {
      this.gz = ''
      if (this.editor) {
        this.editor.children[0].innerHTML = ''
      }

      this.dialogVisible = true
      this.flag = false
      let { data } = await this.$axios.get(`com/textConfig/${id}`)
      this.formData = data.data

      this.gz = this.formData.tzValue || ''
    },
    add() {
      if (this.editor) {
        this.editor.children[0].innerHTML = ''
      }

      this.formData = {
        configCode: 'lb',
        configImg: '',
        configName: null,
        configType: 3,
        configValue: '',
        configwenben: '',
        createTime: null,
        mk: null,
        remark: null,
        sort: null,
        textConfigId: null,
        tzValue: '',
        updateTime: null
      }
      this.dialogVisible = true
      this.flag = true
      this.gz = ''
    },
    update() {
      if (this.editor) {
        this.formData.tzValue = this.editor.html
      }
      this.$refs.ruleForm.validate(async (valid) => {
        let data = null
        if (!valid) return false
        if (!this.formData.textConfigId) {
          data = await this.$axios.post('com/textConfig', this.formData)
          console.log(666)
        } else {
          console.log(777)
          data = await this.$axios.put('com/textConfig', this.formData)
        }

        if (data.data.code != 200) return this.$message.error(data.data.msg)
        this.$message({
          message: data.data.msg,
          type: 'success'
        })
        this.dialogVisible = false
        this.getData()
      })
    },
    // 富文本
    getEidtorText({ html, _this }) {
      if (!this.editor) this.editor = _this
      this.editor.html = html
    },
    handleAvatarSuccess(res, file) {
      this.formData.configValue = res.url
      this.formData.configImg = res.url
    }
  },
  mounted() {
    this.getData()
  }
}
</script>

<style lang="scss" scoped>
.page {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>